package logic;

import interfaces.Observer;

import java.util.ArrayList;
import java.util.List;

import constants.MessageType;

import models.GeneralObject;
import models.Mushroom;
import models.PhysicalObject;
import models.Pig;
import models.Wolf;

import android.graphics.Rect;
import android.util.Log;

/**
 * 
 * @author Merete, Mirna, Esben, Knut og Stian
 * 
 */
public class CollisionHandler {

	private List<GeneralObject> list;
	private List<Observer> observers;

	private static String TAG = CollisionHandler.class.getSimpleName();
	private Pig pig;

	public CollisionHandler(List<GeneralObject> list, List<Observer> listeners) {
		this.list = list;
		observers = listeners;

		if (list.get(1) instanceof Pig) {
			pig = (Pig) list.get(1);
		} else {
			pig = new Pig(null, 0, 0, 0, 0);
		}

		if (listeners.size() != 0) {
			Log.d(TAG, listeners.get(0) + "");
		}
	}

	public void notifyObservers(MessageType mst, Object o) {
		if (observers.size() == 0)
			return;
		for (Observer obs : observers) {
			obs.objectChanged(mst,o);
		}
	}

	public void checkCollisions() {

		// check collision with other objects:
		for (GeneralObject o : list) {
			if (o == null)
				break;

			if(o instanceof Mushroom){
				if (o.isActive() && o.isAffectedbyCollisions() && !o.equals(pig)) {
					if (Rect.intersects(pig.getRect(), o.getRect())) {
						notifyObservers(MessageType.MUSHROOMHIT, o);
						Log.d(TAG,"SENDER COLLISON");
					}
				}	
				
				
			} else if (o instanceof Wolf){
				if (o.isActive() && o.isAffectedbyCollisions() && !o.equals(pig)) {
					if (Rect.intersects(pig.getRect(), o.getRect())) {
						notifyObservers(MessageType.COLLISION, o);
						Log.d(TAG,"SENDER COLLISON");
					}
				}				
			}
			
			
		}

	}
}
